package net.k3rnel.abugadro;

import net.k3rnel.abugadro.util.UIUtils;
import net.k3rnel.abugadro.util.NotifyingAsyncQueryHandler.AsyncQueryListener;

import android.app.Activity;
import android.app.AlertDialog; 
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class Settings extends Activity implements AsyncQueryListener{

	public static final String TAG = "Abugadro Settings";
	public static final String ABUGADRO_PREFS = "abugadro.prefs";
	public static TextView nuke;
	private SharedPreferences prefsPrivate;
	private boolean mEditMode = false;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settings);
		prefsPrivate = getSharedPreferences(ABUGADRO_PREFS, Context.MODE_PRIVATE);
		if(prefsPrivate.getBoolean("Flurry",false))
			((CheckBox) findViewById(R.id.flurry_checkbox)).setChecked(true);
		if(prefsPrivate.getBoolean("RefreshData",true))
			((CheckBox) findViewById(R.id.refresh_checkbox)).setChecked(true);
//		if(prefsPrivate.getBoolean("StoreDB",false))
//			((CheckBox) findViewById(R.id.sdcard_checkbox)).setChecked(true);
		nuke = ((TextView) findViewById(R.id.nukeButton));
		nuke.setLongClickable(true);
		nuke.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				showDialog(R.id.dialog_discard_confirm);
				return false;
			}
		});

	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case R.id.dialog_discard_confirm: {
			return new AlertDialog.Builder(this)
			.setTitle(R.string.settings_nuke_title)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setMessage(R.string.settings_nuke)
			.setNegativeButton(android.R.string.cancel, null)
			.setPositiveButton(android.R.string.ok, new DeleteConfirmClickListener())
			.setCancelable(false)
			.create();
		}
		}
		return null;
	}
	private class DeleteConfirmClickListener implements DialogInterface.OnClickListener {
		public void onClick(DialogInterface dialog, int which) {
			nukeData();
		}
	}
	private void nukeData() {
		prefsPrivate = getSharedPreferences(ABUGADRO_PREFS, Context.MODE_PRIVATE);
		Editor prefsPrivateEditor = prefsPrivate.edit();
		prefsPrivateEditor.putBoolean("Flurry",false);
		prefsPrivateEditor.putBoolean("RefreshData",true);
		prefsPrivateEditor.putBoolean("Flurry-first",true);
		prefsPrivateEditor.putBoolean("Abugadro-first",true);
		prefsPrivateEditor.putBoolean("IssuesList-first",true);
		prefsPrivateEditor.commit();
		
		new AbugadroContentProvider().resetDatabase();
	}
	
	/** Handle "home" title-bar action. */
	public void onHomeClick(View v) {
		UIUtils.goHome(this);
	}
	/** Handle "home" title-bar action. */
	public void onCancelClick(View v) {
		if(!mEditMode)
			UIUtils.goHome(this);
		else{
			showDialog(R.id.dialog_discard_confirm);
		}
	}
	public void onFlurry(View v) {
		prefsPrivate = getSharedPreferences(ABUGADRO_PREFS, Context.MODE_PRIVATE);
		Editor prefsPrivateEditor = prefsPrivate.edit();
		if(((CheckBox) findViewById(R.id.flurry_checkbox)).isChecked()){
			prefsPrivateEditor.putBoolean("Flurry",true);
			Toast.makeText(getApplicationContext(), "Thanks! \nSending Anonymous Usage", Toast.LENGTH_SHORT).show();
		}else{			
			prefsPrivateEditor.putBoolean("Flurry",false);
			Toast.makeText(getApplicationContext(), "Alright. Will no longer phone home.", Toast.LENGTH_SHORT).show();
		}
		prefsPrivateEditor.commit();
	}
	public void onRefreshSummary(View v) {
		prefsPrivate = getSharedPreferences(ABUGADRO_PREFS, Context.MODE_PRIVATE);
		Editor prefsPrivateEditor = prefsPrivate.edit();
		if(((CheckBox) findViewById(R.id.refresh_checkbox)).isChecked()){
			prefsPrivateEditor.putBoolean("RefreshData",true);
			Toast.makeText(getApplicationContext(), "Data will be fetched automatically", Toast.LENGTH_SHORT).show();
		}else{			
			prefsPrivateEditor.putBoolean("RefreshData",false);
			Toast.makeText(getApplicationContext(), "You'll have to hit the Refresh button to fetch data.", Toast.LENGTH_LONG).show();
		}
		prefsPrivateEditor.commit();
	}

	@Override
	public void onQueryComplete(int token, Object cookie, Cursor cursor) {
		// TODO Auto-generated method stub
		
	}
	
}